package com.solidtrust.crawler.service;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.solidtrust.app.FileSystemUtil;
import com.solidtrust.crawler.model.PageSource;
import com.solidtrust.entity.Brand;
import com.solidtrust.entity.Build;
import com.solidtrust.entity.Model;
import com.solidtrust.entity.Volume;
import com.solidtrust.repository.BrandRepository;
import com.solidtrust.repository.BuildRepository;
import com.solidtrust.repository.ModelRepository;
import com.solidtrust.repository.VolumeRepository;
import com.solidtrust.util.DateUtil;

@Service
public class PriceMetaDataService {
	
	private static final Logger log4j = Logger.getLogger(PriceMetaDataService.class);
	private static final String BRAND_MODEL_URL = "http://bj.ganji.com/ershouche/pinpai/";
	private static final int TIMEOUT = 1000 * 10;
	
	@Autowired BrandRepository brandRep;
	@Autowired ModelRepository modelRep;
	@Autowired BuildRepository buildRep;
	@Autowired VolumeRepository volumeRep;
	@PersistenceContext EntityManager em;
	@Autowired GanjiService ganji;
	
	@Deprecated
	public void brand_model() throws IOException{
		Document doc = Jsoup.parse(new URL(BRAND_MODEL_URL), TIMEOUT);
		Elements brands = doc.select(".py-brand");
		
		File file = FileSystemUtil.getBrandLogo();
		
		for (int i = 0; i < brands.size(); i++) {
			Element ele = brands.get(i);
			Brand brand = new Brand();
			
			String name = ele.select(".hb-name").text();
			brand.setBrand(name);
			
			String id = ele.select(".hb-genre a").first().attr("href");
			String py = id.replace("/", "");
			brand.setPinyin(py);
			
			File f = new File(file, py + ".jpg");
			if(!f.isFile()){
				String logoUrl = ele.select(".hb-logo img").attr("src");
				try {
					FileUtils.copyURLToFile(new URL(logoUrl), f);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			brand.setSource("ganji");
			brand.setSourceId(id);
			
			brandRep.save(brand);
			
			//model
			Elements modelEles = ele.select(".py-width li");
			for (int j = 0; j < modelEles.size(); j++) {
				ele = modelEles.get(j);
				Model model = new Model();
				
				String name1 = ele.child(0).text();
				model.setModel(name1);
				
				String id1 = ele.select("span > a").first().attr("href");
				String py1 = id1.replace("/", "").replace(py + "-", "");
				model.setPinyin(py1);
				
				model.setSource("ganji");
				model.setSourceId(id1);
				
				model.setBrand(brand);
				modelRep.save(model);
			}
		}
	}
	
	/*
	 * brand_model获取的brand和model不全，model从主页面直接扒取
	 */
	public void brand_model(Integer start) throws IOException{
		List<Brand> brands = brandRep.findAll();
		if(brands.isEmpty()){
			for (PageSource pageSource : ganji.getBrands()) {
				Brand b = new Brand();
				b.setBrand(pageSource.getName());
				b.setSourceId(pageSource.getOriginalUrl());
				b.setPinyin(b.getSourceId().replace("/", ""));
				b.setSource("ganji");
				brandRep.save(b);
			}
			brands = brandRep.findAll();
		}
		
		log4j.info("Brands:" + brands.size());
		for (Brand b : brands) {
			if(start == null || b.getId() >= start){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				String py = b.getPinyin();
				String url = "http://yangzhou.ganji.com" + b.getSourceId();
				log4j.info(url);
				Document doc = Jsoup.parse(new URL(url), TIMEOUT);
				Elements eles = doc.select(".posrelative").first().select("a");
				for (Element ele : eles) {
					String name = ele.text();
					if(name.equals("不限") || name.equals("更多")){
						continue;
					}
					
					Model model = new Model();
					model.setModel(name);
					
					String id1 = ele.attr("href");
					String py1 = id1.replace("/", "").replace(py + "-", "");
					model.setPinyin(py1);
					
					model.setSource("ganji");
					model.setSourceId(id1);
					
					model.setBrand(b);
					modelRep.save(model);
				}
			}
		}
	}
	
	public void build_volume(){
		Date d = DateUtils.addMonths(new Date(), -4);
		int year = Calendar.getInstance().get(Calendar.YEAR);
		log4j.info("from " + DateUtil.date2String2(d));
		List<Model> models = modelRep.findAll();
		String ql = "select g.build, g.volume, count(g) from Ganji g "
				+ "where g.scrapedDate > :date and g.brand = :brand and g.model = :model and (g.build < "+year+" and g.build > 1900) and g.volume != 0 "
				+ "group by g.build, g.volume "
				+ "having count(g) >= 10 "
				+ "order by g.build desc, g.volume asc";
		
		for (Model model : models) {
			List<Object[]> list = em.createQuery(ql, Object[].class)
				.setParameter("brand", model.getBrand().getBrand())
				.setParameter("model", model.getModel())
				.setParameter("date", d)
				.getResultList();
			Map<String, Build> buildMap = new HashMap<>();
			for (Object[] array : list) {
				String build = (String) array[0];
				if(!StringUtils.isNumeric(build)){
					continue;
				}
				Integer volume = (Integer) array[1];
				Long count = (Long) array[2];
				Build b = buildMap.get(build);
				if(b == null){
					b = new Build();
					b.setBuild(build);
					b.setModel(model);
					buildRep.save(b);
					buildMap.put(build, b);
				}
				
				Volume v = new Volume();
				v.setBuild(b);
				v.setVolume(volume);
				v.setCount(count.intValue());
				volumeRep.save(v);
			}
		}
	}
	
	
}
